import * as Cesium from '@cesiumjs';

function createODLineAppearance(_colorArr, translucent, image) {
  _colorArr = _colorArr || [0.8, 0.8, 0x0, 0x1];

  var appearance;
  const color = new Cesium.Color(Cesium.Cartesian4.fromArray(_colorArr));
  var fabric = image ? {
    'type': "Image",
    'uniforms': {
      color,
      image
    }
  } : {
    'type': "Color",
    'uniforms': {
      color
    }
  };

  translucent = !!translucent;

  if (translucent) {
    appearance = new Cesium.PolylineMaterialAppearance({
      'material': new Cesium.Material({
        fabric,
        translucent
      })
    });
  } else {
    appearance = new Cesium.PolylineMaterialAppearance({
      'material': new Cesium.Material({
        fabric,
        translucent
      }),
      translucent
    });
    appearance.renderState.blending = Cesium.BlendingState.ADDITIVE_BLEND;
    appearance.renderState.depthMask = ![];
    appearance.renderState.depthTest = {};
    appearance.renderState.depthTest.enabled = !![];
    appearance.getRenderState = function() {
      var _0x22a864 = Cesium.Appearance.prototype.getRenderState.call(this);
      _0x22a864.depthTest = {
        enabled: true
      };
      _0x22a864.depthMask = false;
      return _0x22a864;
    };
  }

  var _0x57e44b = Cesium.ShaderSource.replaceMain(appearance._vertexShaderSource, 'czm_twp_main');

  // var _0x31b19f = _0x44d5('0x2918', 'RAiX') + _0x44d5('0x2919', ')6Ka') + _0x44d5('0x291a', 'uK^Z') + _0x44d5(
  //   '0x291b', '*&l7') + _0x44d5('0x291c', 'Vs1%') + _0x44d5('0x291d', 'NrAr') + _0x44d5('0x291e', 'WEn[') + '}';

  var _0x31b19f = `
        varying vec4 v_twp;
        varying vec4 v_color;
        void main()
        {
           czm_twp_main();
           v_twp = czm_batchTable_twp(batchId);
           v_color = czm_batchTable_color(batchId);
        }
        `;

  appearance._vertexShaderSource = _0x57e44b + '\x0a' + _0x31b19f;

  var _0x1e5553 = Cesium.ShaderSource.replaceMain(appearance._fragmentShaderSource, 'czm_twp2_main');

  const fs = `
        varying vec4 v_twp;
        varying vec4 v_color;
        void main(){
          czm_twp2_main();
          float t = v_twp.x;
          t *= 1.05; //移动点长短
          float alpha = smoothstep(t - 0.025, t, v_st.s) * step(-t, -v_st.s);
          alpha += 0.2; //线透明度
          gl_FragColor.rgb *= v_color.rgb;
          gl_FragColor.a *= alpha;
        }`;

  const fsImage = `
        varying vec4 v_twp;
        varying vec4 v_color;
        void main(){
          czm_twp2_main();
          float t = v_twp.x;
          float repeat = v_twp.y;
          gl_FragColor = texture2D(image_1, fract(vec2(fract((v_st.s-t)*repeat), v_st.t)));
          gl_FragColor *= color_0;
          gl_FragColor *= v_color;
        }`;

  var _0x546926 = image ? fsImage : fs;
  appearance._fragmentShaderSource = _0x1e5553 + '\x0a' + _0x546926;
  return appearance;
}

export default createODLineAppearance;
